//
//  RecommendViewController.m
//  网络FM
//
//  Created by dengzhicheng on 15/5/21.
//  Copyright (c) 2015年 dengzhicheng.com. All rights reserved.
//

#import "RecommendViewController.h"
#import "RecommendTableViewCell.h"
#import <AFNetworking.h>
#import "RecommendModel.h"
#import <UIImageView+WebCache.h>
#import "RecommendListViewController.h"
@interface RecommendViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
//定义数组属性 接收model
@property (nonatomic, retain)NSMutableArray *recommendArray;

@end

@implementation RecommendViewController
- (void)dealloc
{
    [_tableView release];
    [_recommendArray release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //初始化数组
        self.recommendArray =[NSMutableArray array];
    }
    return self;
}
//创建tableView
- (void)recommendTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    //头部图片添加
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 4)];
    [imageView setImage:[UIImage imageNamed:@"分类界面图片.png"]];
    _tableView.tableHeaderView = imageView;
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
}

- (void)loadView
{
    [super loadView];
    [self recommendConnection];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.translucent = NO;
}


#pragma mark -
#pragma mark Section
//定义分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *str = [NSString string];
    if (section == 0) {
        str = @"排行总榜";
    } else {
        str = @"内容精选";
    }
    return str;
}

#pragma mark -
#pragma mark 创建Cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    } else {
        return 5;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"RecommendCell";
    RecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[RecommendTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
    }
    
    //判断是哪一组的Cell
    if (indexPath.section == 0) {
        RecommendModel *model = [self.recommendArray objectAtIndex:indexPath.row];
        [cell.cellImageView sd_setImageWithURL:[NSURL URLWithString:model.img100_100] placeholderImage:[UIImage imageNamed:@"占位图.jpg"] options:SDWebImageRetryFailed];
        [cell.cellFirstLable setText:model.topTitle];
        [cell.cellSecondLable setText:model.topDesc];
    } else {
        RecommendModel *model = [self.recommendArray objectAtIndex:indexPath.row + 3];
        [cell.cellImageView sd_setImageWithURL:[NSURL URLWithString:model.img100_100] placeholderImage:[UIImage imageNamed:@"占位图.jpg"] options:SDWebImageRetryFailed];
        [cell.cellFirstLable setText:model.topTitle];
        [cell.cellSecondLable setText:model.topDesc];
    }
    
    return cell;
}
//cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}



#pragma mark -
#pragma mark Cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendListViewController *list = [[RecommendListViewController alloc] init];
    if (indexPath.section == 0) {
        RecommendModel *model = [self.recommendArray objectAtIndex:indexPath.row];
        list.topTitle = model.topTitle;
        list.lbid = model.ID;
        list.pagenum = model.sort;
    } else {
    RecommendModel *model = [self.recommendArray objectAtIndex:indexPath.row + 3];
        list.topTitle = model.topTitle;
        list.lbid = model.ID;
        list.pagenum = model.sort;
    }
    [self.navigationController pushViewController:list animated:YES];
    [list release];
}




#pragma mark - 
#pragma mark 数据请求
- (void)recommendConnection
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"http://fm.ifeng.com/fm/read/fmd/ios/600/getLeaderboard.html?appid=TRWLY&sign=58053A9505A7789DC944C95B0D782E9E&timestamp=1432019736&token=NyYDDrwTrhZ4b3hi"];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [bigDic objectForKey:@"data"];
        NSMutableDictionary *dic1 = [array firstObject];
        
        //遍历存入model放入数组
        NSMutableArray *arr1 = [dic1 objectForKey:@"bdList"];
        for (NSDictionary *dictionary in arr1) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:dictionary];
            [self.recommendArray addObject:model];
            [model release];
         }
        NSMutableDictionary *dic2 = [array lastObject];
        NSMutableArray *arr2 = [dic2 objectForKey:@"bdList"];
        [arr2 removeLastObject];
        for (NSDictionary *dictionary in arr2) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:dictionary];
            [self.recommendArray addObject:model];
            [model release];
        }
        //请求完毕再创建tableView
        [self recommendTableView];
    } failure:
     ^(AFHTTPRequestOperation *operation, NSError *error) {
         
     }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
